//
//  SysDicModel.m
//
//  Created by   on 2019/6/4
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "SysDicModel.h"


NSString *const kSysDicModelDictName = @"dictName";
NSString *const kSysDicModelId = @"id";
NSString *const kSysDicModelIsDelete = @"isDelete";
NSString *const kSysDicModelUpdateTime = @"updateTime";
NSString *const kSysDicModelDictCode = @"dictCode";
NSString *const kSysDicModelDictTypeCode = @"dictTypeCode";
NSString *const kSysDicModelAddTime = @"addTime";
NSString *const kSysDicModelUpdateUser = @"updateUser";
NSString *const kSysDicModelAddUser = @"addUser";


@interface SysDicModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SysDicModel

@synthesize dictName = _dictName;
@synthesize internalBaseClassIdentifier = _internalBaseClassIdentifier;
@synthesize isDelete = _isDelete;
@synthesize updateTime = _updateTime;
@synthesize dictCode = _dictCode;
@synthesize dictTypeCode = _dictTypeCode;
@synthesize addTime = _addTime;
@synthesize updateUser = _updateUser;
@synthesize addUser = _addUser;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.dictName = [self objectOrNilForKey:kSysDicModelDictName fromDictionary:dict];
            self.internalBaseClassIdentifier = [[self objectOrNilForKey:kSysDicModelId fromDictionary:dict] doubleValue];
            self.isDelete = [[self objectOrNilForKey:kSysDicModelIsDelete fromDictionary:dict] boolValue];
            self.updateTime = [self objectOrNilForKey:kSysDicModelUpdateTime fromDictionary:dict];
            self.dictCode = [self objectOrNilForKey:kSysDicModelDictCode fromDictionary:dict];
            self.dictTypeCode = [self objectOrNilForKey:kSysDicModelDictTypeCode fromDictionary:dict];
            self.addTime = [[self objectOrNilForKey:kSysDicModelAddTime fromDictionary:dict] doubleValue];
            self.updateUser = [self objectOrNilForKey:kSysDicModelUpdateUser fromDictionary:dict];
            self.addUser = [self objectOrNilForKey:kSysDicModelAddUser fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.dictName forKey:kSysDicModelDictName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.internalBaseClassIdentifier] forKey:kSysDicModelId];
    [mutableDict setValue:[NSNumber numberWithBool:self.isDelete] forKey:kSysDicModelIsDelete];
    [mutableDict setValue:self.updateTime forKey:kSysDicModelUpdateTime];
    [mutableDict setValue:self.dictCode forKey:kSysDicModelDictCode];
    [mutableDict setValue:self.dictTypeCode forKey:kSysDicModelDictTypeCode];
    [mutableDict setValue:[NSNumber numberWithDouble:self.addTime] forKey:kSysDicModelAddTime];
    [mutableDict setValue:self.updateUser forKey:kSysDicModelUpdateUser];
    [mutableDict setValue:self.addUser forKey:kSysDicModelAddUser];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.dictName = [aDecoder decodeObjectForKey:kSysDicModelDictName];
    self.internalBaseClassIdentifier = [aDecoder decodeDoubleForKey:kSysDicModelId];
    self.isDelete = [aDecoder decodeBoolForKey:kSysDicModelIsDelete];
    self.updateTime = [aDecoder decodeObjectForKey:kSysDicModelUpdateTime];
    self.dictCode = [aDecoder decodeObjectForKey:kSysDicModelDictCode];
    self.dictTypeCode = [aDecoder decodeObjectForKey:kSysDicModelDictTypeCode];
    self.addTime = [aDecoder decodeDoubleForKey:kSysDicModelAddTime];
    self.updateUser = [aDecoder decodeObjectForKey:kSysDicModelUpdateUser];
    self.addUser = [aDecoder decodeObjectForKey:kSysDicModelAddUser];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_dictName forKey:kSysDicModelDictName];
    [aCoder encodeDouble:_internalBaseClassIdentifier forKey:kSysDicModelId];
    [aCoder encodeBool:_isDelete forKey:kSysDicModelIsDelete];
    [aCoder encodeObject:_updateTime forKey:kSysDicModelUpdateTime];
    [aCoder encodeObject:_dictCode forKey:kSysDicModelDictCode];
    [aCoder encodeObject:_dictTypeCode forKey:kSysDicModelDictTypeCode];
    [aCoder encodeDouble:_addTime forKey:kSysDicModelAddTime];
    [aCoder encodeObject:_updateUser forKey:kSysDicModelUpdateUser];
    [aCoder encodeObject:_addUser forKey:kSysDicModelAddUser];
}

- (id)copyWithZone:(NSZone *)zone
{
    SysDicModel *copy = [[SysDicModel alloc] init];
    
    if (copy) {

        copy.dictName = [self.dictName copyWithZone:zone];
        copy.internalBaseClassIdentifier = self.internalBaseClassIdentifier;
        copy.isDelete = self.isDelete;
        copy.updateTime = [self.updateTime copyWithZone:zone];
        copy.dictCode = [self.dictCode copyWithZone:zone];
        copy.dictTypeCode = [self.dictTypeCode copyWithZone:zone];
        copy.addTime = self.addTime;
        copy.updateUser = [self.updateUser copyWithZone:zone];
        copy.addUser = [self.addUser copyWithZone:zone];
    }
    
    return copy;
}


@end
